//
//  MLATabBarVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/14.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLATabBarVC.h"
#import "MLABaseNavController.h"
#import "MLAHomeVC.h"
#import "MLAMineVC.h"

#import "MLAOrderFoodHomeVC.h"
#import "MLAEateryHomeVC.h"

#import "MLALoginVC.h"

static CGFloat const CYLTabBarControllerHeight = 40.f;

@interface MLATabBarVC () <CYLTabBarControllerDelegate>

@end

@implementation MLATabBarVC

- (instancetype)init {
    if (!(self = [super init])) {
        return nil;
    }
    
    /**
     * 以下两行代码目的在于手动设置让TabBarItem只显示图标，不显示文字，并让图标垂直居中。
     * 等效于在 `-tabBarItemsAttributesForController` 方法中不传 `CYLTabBarItemTitle` 字段。
     * 更推荐后一种做法。
     */
    UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
    UIOffset titlePositionAdjustment = UIOffsetMake(0, -3.5);
    CYLTabBarController *tabBarController =
    [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers
                                       tabBarItemsAttributes:self.tabBarItemsAttributesForController
                                                 imageInsets:imageInsets
                                     titlePositionAdjustment:titlePositionAdjustment
                                                     context:nil];
    tabBarController.delegate = (id)self;
    [self customizeTabBarAppearance:tabBarController];
    return (self = (MLATabBarVC *)tabBarController);
}

- (NSArray *)tabBarItemsAttributesForController {
    
    NSDictionary *homeAttri = @{CYLTabBarItemTitle : @"首页",
                                CYLTabBarItemImage : @"tab_home_n",
                                CYLTabBarItemSelectedImage : @"tab_home_s"};
    
    NSDictionary *foodAttri = @{CYLTabBarItemTitle : @"点餐",
                                CYLTabBarItemImage : @"tab_order_n",
                                CYLTabBarItemSelectedImage : @"tab_order_s"};
    
    NSDictionary *mineAttri = @{CYLTabBarItemTitle : @"我的",
                                CYLTabBarItemImage : @"tab_mine_n",
                                CYLTabBarItemSelectedImage : @"tab_mine_s"};

    NSArray *attri = @[homeAttri,foodAttri,/*storeAttri,*/mineAttri];
    return attri;
}

- (NSArray *)viewControllers {
    
    NSMutableArray *vcsArr = [NSMutableArray array];
    
    MLAHomeVC *homeVC = [[MLAHomeVC alloc] init];
    MLABaseNavController *homeNav = [[MLABaseNavController alloc] initWithRootViewController:homeVC];
    [vcsArr addObject:homeNav];

    MLABaseNavController *foodNav;
    if ([[CX_GetUserDefaults() objectForKey:@"isShop"] integerValue] == 1) {
        MLAOrderFoodHomeVC *vc = [[MLAOrderFoodHomeVC alloc] init];
        foodNav = [[MLABaseNavController alloc] initWithRootViewController:vc];
    } else {
        MLAEateryHomeVC *vc = [[MLAEateryHomeVC alloc] init];
        foodNav = [[MLABaseNavController alloc] initWithRootViewController:vc];
    }
    [vcsArr addObject:foodNav];
    
    MLAMineVC *mineVC = [[MLAMineVC alloc] init];
    MLABaseNavController *mineNav = [[MLABaseNavController alloc] initWithRootViewController:mineVC];
    [vcsArr addObject:mineNav];

    
    return [vcsArr copy];
}

/**
 *  更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
 */
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
    // Customize UITabBar height
    // 自定义 TabBar 高度
    //    tabBarController.tabBarHeight = CYL_IS_IPHONE_X ? 65 : 40;
    
    // set the text color for unselected state
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    // set the text color for selected state
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = CX_GetColorBlue();
    
    // set the text Attributes
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    // Set the dark color to selected tab (the dimmed background)
    // TabBarItem选中后的背景颜色
    [self customizeTabBarSelectionIndicatorImage];
    
    // update TabBar when TabBarItem width did update
    // If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight，
    // remove the comment '//'
    // 如果你的App需要支持横竖屏，请使用该方法移除注释 '//'
    [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
    
    // set the bar shadow image
    // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundColor:[UIColor clearColor]];
    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    //        [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
    
    // set the bar background image
    // 设置背景图片
    UITabBar *tabBarAppearance = [UITabBar appearance];
    
    //FIXED:  https://github.com/ChenYilong/CYLTabBarController/issues/312
    [UITabBar appearance].translucent = NO;
    //FIXED: https://github.com/ChenYilong/CYLTabBarController/issues/196
    NSString *tabBarBackgroundImageName = @"tabbarBg";
    UIImage *tabBarBackgroundImage = [UIImage imageNamed:tabBarBackgroundImageName];
    UIImage *scanedTabBarBackgroundImage = [[self class] scaleImage:tabBarBackgroundImage];
    [tabBarAppearance setBackgroundImage:scanedTabBarBackgroundImage];
    
    // remove the bar system shadow image
    // 去除 TabBar 自带的顶部阴影
    // iOS10 后 需要使用 `-[CYLTabBarController hideTabBadgeBackgroundSeparator]` 见 AppDelegate 类中的演示;
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
}

- (void)updateTabBarCustomizationWhenTabBarItemWidthDidUpdate {
    void (^deviceOrientationDidChangeBlock)(NSNotification *) = ^(NSNotification *notification) {
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        if ((orientation == UIDeviceOrientationLandscapeLeft) || (orientation == UIDeviceOrientationLandscapeRight)) {
            NSLog(@"Landscape Left or Right !");
        } else if (orientation == UIDeviceOrientationPortrait) {
            NSLog(@"Landscape portrait!");
        }
        [self customizeTabBarSelectionIndicatorImage];
    };
    [[NSNotificationCenter defaultCenter] addObserverForName:CYLTabBarItemWidthDidChangeNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:deviceOrientationDidChangeBlock];
}

- (void)customizeTabBarSelectionIndicatorImage {
    ///Get initialized TabBar Height if exists, otherwise get Default TabBar Height.
    CGFloat tabBarHeight = CYLTabBarControllerHeight;
    CGSize selectionIndicatorImageSize = CGSizeMake(CYLTabBarItemWidth, tabBarHeight);
    //Get initialized TabBar if exists.
    UITabBar *tabBar = [self cyl_tabBarController].tabBar ?: [UITabBar appearance];
    [tabBar setSelectionIndicatorImage:
     [[self class] imageWithColor:[UIColor whiteColor]
                             size:selectionIndicatorImageSize]];
}

+ (UIImage *)scaleImage:(UIImage *)image {
    CGFloat halfWidth = image.size.width/2;
    CGFloat halfHeight = image.size.height/2;
    UIImage *secondStrechImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(halfHeight, halfWidth, halfHeight, halfWidth) resizingMode:UIImageResizingModeStretch];
    return secondStrechImage;
}


+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    [[self cyl_tabBarController] updateSelectionStatusIfNeededForTabBarController:tabBarController shouldSelectViewController:viewController];
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectControl:(UIControl *)control {
    
    NSInteger index = tabBarController.selectedIndex;
    NSString *token = [CX_GetUserDefaults() objectForKey:kAccessToken];
    
    if (index != 0 && !token) {
        tabBarController.selectedIndex = 0;
        MLALoginVC *loginVC = [[MLALoginVC alloc] init];
        [tabBarController.selectedViewController pushViewController:loginVC animated:YES];
    }
}


@end
